package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.system.domain.SysGm;
import com.ruoyi.system.service.ISysGmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * GM 信息操作处理
 *
 * @author ruoyi
 */
@Api("GM信息操作处理")
@RestController
@RequestMapping("/system/gm")
public class SysGmController extends BaseController {
    @Autowired
    private ISysGmService gmService;

    /**
     * 获取道具列表
     */
    @ApiOperation("获取道具列表")
    @GetMapping("/list")
    public TableDataInfo list(SysGm gm) {
        startPage();
        List<SysGm> list = gmService.selectGmList(gm);
        return getDataTable(list);
    }

    /**
     * 获取道具列表(app)
     */
    @ApiOperation("获取道具列表")
    @GetMapping("/applist")
    public TableDataInfo applist(SysGm gm) {
        startPage();
        List<SysGm> list = gmService.selectGmList(gm);
        List<SysGm> listS = new ArrayList<>();
        for (SysGm sysGm : list) {
            if (sysGm.getStatus().equals("0")) {
                listS.add(sysGm);
            }
        }
        return getDataTable(listS);
    }


    /**
     * 根据道具编号获取详细信息
     */
    @ApiOperation("根据道具编号获取详细信息")
    @ApiImplicitParam(name = "payload_id", value = "道具编号", required = true, dataType = "Long", paramType = "path")
    @GetMapping(value = "/{payload_id}")
    public AjaxResult getInfo(@PathVariable Long payload_id) {
        return AjaxResult.success(gmService.selectGmById(payload_id));
    }

    /**
     * 新增道具
     */
    @ApiOperation("新增道具")
    @Log(title = "道具", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysGm gm) {
        gm.setAppid("200002");
        gm.setCurrency("CNY");
        gm.setFpid("1851027487");
        gm.setOrder_id("162414377" + getRandom(9));
        gm.setSign("2f82177338de601981a1100a0cd1e0e8");
        gm.setTransaction_id("sandbox2021062007025" + getRandom(9));
        gm.setGame_uid("2850086000411478419");
        gm.setPurchase_token("0000017a268355b4ee1d2f4afc5d965031845e4c1959a" + Md5Utils.stringToMD5(String.valueOf(new Date().getTime()) + getRanNum(), 32) + "426x434e.1.100030305");
        switch (gm.getGoods_type()) {
            case "0":
                gm.setAmount("6.00");
                break;
            case "1":
                gm.setAmount("30.00");
                break;
            case "2":
                gm.setAmount("68.00");
                break;
            case "3":
                gm.setAmount("128.00");
                break;
            case "4":
                gm.setAmount("328.00");
                break;
            case "5":
                gm.setAmount("648.00");
                break;
            default:
                break;
        }
        gm.setChannel("huawei");
        gm.setStatus("0");
        return toAjax(gmService.insertGm(gm));
    }

    private int getRanNum() {
        int max = 10000000, min = 1;
        return (int) (Math.random() * (max - min) + min);
    }

    public static String getRandom(int len) {
        int rs = (int) ((Math.random() * 9 + 1) * Math.pow(10, len - 1));
        return String.valueOf(rs);
    }

    /**
     * 修改道具
     */
    @ApiOperation("修改道具")
    @Log(title = "道具", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysGm gm) {
        return toAjax(gmService.updateGm(gm));
    }

    /**
     * 修改and新增 app
     */
    @ApiOperation("道具修改app")
    @Log(title = "道具修改app", businessType = BusinessType.UPDATE)
    @PostMapping("/appupdate")
    public AjaxResult appupdate(@Validated @RequestBody SysGm gm) {
        return toAjax(gmService.updateGm(gm));
    }

    /**
     * 修改and新增 app
     */
    @ApiOperation("道具添加app")
    @Log(title = "道具添加app", businessType = BusinessType.UPDATE)
    @PostMapping("/appadd")
    public AjaxResult appadd(@Validated @RequestBody SysGm gm) {
        gm.setAppid("200002");
        gm.setCurrency("CNY");
        gm.setFpid("1851027487");
        gm.setOrder_id("162414377" + getRandom(9));
        gm.setSign("2f82177338de601981a1100a0cd1e0e8");
        gm.setTransaction_id("sandbox2021062007025" + getRandom(9));
        gm.setGame_uid("2850086000411478419");
        gm.setPurchase_token("0000017a268355b4ee1d2f4afc5d965031845e4c1959a" + Md5Utils.stringToMD5(String.valueOf(new Date().getTime()) + getRanNum(), 32) + "426x434e.1.100030305");
        switch (gm.getGoods_type()) {
            case "0":
                gm.setAmount("6.00");
                break;
            case "1":
                gm.setAmount("30.00");
                break;
            case "2":
                gm.setAmount("68.00");
                break;
            case "3":
                gm.setAmount("128.00");
                break;
            case "4":
                gm.setAmount("328.00");
                break;
            case "5":
                gm.setAmount("648.00");
                break;
            default:
                break;
        }
        gm.setChannel("huawei");
        gm.setStatus("0");
        return toAjax(gmService.insertGm(gm));
    }



    /**
     * 删除道具
     */
    @ApiOperation("删除道具")
    @Log(title = "道具", businessType = BusinessType.DELETE)
    @DeleteMapping("/{payload_ids}")
    public AjaxResult remove(@PathVariable Long[] payload_ids) {
        return toAjax(gmService.deleteGmByIds(payload_ids));
    }

    /**
     * 状态修改
     */
    @Log(title = "道具", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@Validated @RequestBody SysGm gm) {
        return toAjax(gmService.updateStatus(gm));
    }
}
